﻿using PLCSharp.CoreModel.Prism;
using PLCSharp.Models;
using Prism.Commands;
using Prism.Dialogs;
using Prism.Events;
using Prism.Ioc;

namespace PLCSharp.Views.Homepage
{
    public class HomeEditViewModel : DialogAwareBase
    {
        public HomeEditViewModel(IContainerExtension container, IEventAggregator ea, IDialogService dialogService) : base(container, ea, dialogService)

        {
            Model = container.Resolve<GlobalModel>();
        }
        public GlobalModel Model { get; }

        private DelegateCommand<string> _Manage;
        public DelegateCommand<string> Manage =>
            _Manage ??= new DelegateCommand<string>(ExecuteManage);

        void ExecuteManage(string cmd)
        {
            switch (cmd)
            {
                case "New":
                    Model.CustomControls.Add(new CustomControl() { RecipeID = Model.CurrentRecipe.ID });

                    break;

                case "Save":
                    var names = new List<string>();

                    foreach (var item in Model.CustomControls)
                    {
                        if (string.IsNullOrEmpty(item.Name))
                        {
                            Model.SendErr($"保存失败，名称{item.Name}不合适！", ErrType.ShowDialog);
                            return;
                        }

                        if (names.Contains(item.Name))
                        {
                            Model.SendErr($"保存失败，重复的名称{item.Name}！", ErrType.ShowDialog);
                            return;
                        }
                        else
                        {
                            names.Add(item.Name);
                        }
                    }


                    foreach (var item in Model.CustomControls)
                    {
                        if (!Model._DatasContext.CustomControls.Any(h => h.ID == item.ID))
                        {
                            Model._DatasContext.CustomControls.Add(item);

                        }
                        else
                        {
                            var control = Model._DatasContext.CustomControls.Where(c => c.ID == item.ID).FirstOrDefault();
                            control.RecipeID = item.RecipeID;
                            control.Name = item.Name;
                            control.Type = item.Type;
                            control.Row = item.Row;
                            control.RowSpan = item.RowSpan;
                            control.Column = item.Column;
                            control.ColumnSpan = item.ColumnSpan;
                            control.Params = item.Params;
                            control.Comment = item.Comment;
                        }


                    }
                    Model.SelectedCustomControl.Prompt = "";

                    break;

                case "Config":
                    var dialogParams = new DialogParameters
                        {
                            { "SelectedCustomControl", Model.SelectedCustomControl }
                        };
                    Model._dialogService.Show("CustomControlConfig", dialogParams, r =>
                    {


                    });
                    break;

                case "Remove":
                    if (Model.SelectedCustomControl != null)
                    {
                        var control = Model._DatasContext.CustomControls.Where(c => c.ID == Model.SelectedCustomControl.ID).FirstOrDefault();
                        if (control != null)
                        {

                            Model._DatasContext.CustomControls.Remove(control);
                        }
                        var name = Model.SelectedCustomControl.Name;
                        Model.CustomControls.Remove(Model.SelectedCustomControl);
                        Model.SendErr($"已删除：{name}", ErrType.ShowDialog);
                        Model._DatasContext.Save();
                    }
                    break;

            }
        }
    }
}